<template>
	<view class="home-page">
		<!-- 顶部导航栏（红金主题适配） -->
		<view class="top-nav">
			<view class="tab-top">
				<view class="nav-tab1" :class="{ active: currentTab === 'self' }" @click="currentTab = 'self'">自取</view>
				<view class="nav-tab1" :class="{ active: currentTab === 'delivery' }" @click="currentTab = 'delivery'">外卖</view>
			</view>
			<!-- <view class="tab-search"></view> -->
			<view class="search-icon">
				<u-icon name="search" size="24" color="#000000" @click="goSearch"></u-icon>
			</view>
		</view>
		<!-- 轮播图区域（优化渐变覆盖） -->
		<u-swiper height="240" :list="bannerList"></u-swiper>

		<!-- 店铺信息区域（红金主题重构） -->
		<view class="store-info">
			<view class="store-header" @click="getAddress">
				<text class="star-icon">★</text>
				<text class="store-title">观澜老街店</text>
				<u-icon name="arrow-right" size="20" class="arrow-icon"></u-icon>
			</view>
			<view class="store-meta">
				<text class="make-tag">下单立即制作</text>
				<text class="divider">|</text>
				<text class="distance-tag">距离您536m</text>
			</view>
			<view class="coupon-list">
				<text class="coupon-item" v-for="(coupon, idx) in couponList" :key="idx">{{ coupon }}</text>
			</view>
		</view>

		<!-- 活动横幅（保持简洁，可替换为动态数据） -->
		<!-- <image src="/static/images/activity-banner.png" mode="widthFix" class="activity-banner"></image> -->
		<view class="parent">
			<!-- 分类栏 -->
			<scroll-view scroll-y="true" style="height: 45vh; width: 24%;">
				<view class="category-nav" ref="scrollContainer">
					<view class="category-item" v-for="(category, cIdx) in categoryList" :key="cIdx"
						:class="{ active: currentIndex === cIdx }" @click="clickCategory(cIdx)"
						:style="{transform: 'translateY(' + scrollY + 'rpx)'}">
						<u-icon :label="category.name" labelPos="bottom" size="30" :name="category.url_text"></u-icon>
					</view>
				</view>
			</scroll-view>
			<!-- 商品列表栏 -->
			<scroll-view scroll-y="true" style="height: 45vh; width: 74%;">
				<view class="goods-list">
					<view class="goods-item" v-for="(goods, gIdx) in goodsList" :key="gIdx"
						@touchstart="handlePress(goods, $event)" @touchend="handleRelease">
						<view style="width: 30%;">
							<image :src="goods.url_text" mode="widthFix" class="goods-img"></image>
						</view>
						<view style="width: 65%;" class="goods-info">
							<text class="goods-name">{{ goods.name }}</text>
							<view class="tags">
								<text class="tag" v-for="(tag, tIdx) in goods.tag" :key="tIdx">{{ tag }}</text>
							</view>
							<text class="desc">{{ goods.description }}</text>
							<view class="price">
								<text class="current-price">¥{{ goods.currentPrice }}到手价<text class="original-price"
										v-if="goods.base_price">¥{{ goods.base_price }} 起</text></text>
								<u-icon @click="show = true" size="20" color="#bb78ba" name="plus-square-fill"></u-icon>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<!-- <view> -->
		<u-popup :show="show" @close="close" @open="open" safeAreaInsetBottom="false" overlay="false" duration="300">
			<scroll-view scroll-y="true" style="height: 85vh; width: 100%;" @scroll="onScroll">
				<view style="display: flex; flex-direction: column;">
					<u--image :showLoading="true" src="/static/banners/banner01.png" width="100%"></u--image>
					<view class="goName">云雾栀子</view>
					<u-sticky>
						<view v-if="isImageAbove===true" style="display: flex;flex-direction: column;">
							<view class="sm-goods">
								<u--image :showLoading="true" src="/static/goods/good01.png" width="100px" height="100px" @click="click"
									radius="10"></u--image>
								<view class="sm-gtitle">
									<view class="tit">云雾栀子</view>
									<view class="txt">大、少冰、默认糖、七分甜(推荐)</view>
								</view>
							</view>
							<view class="gs-title">
								<view>口味选择</view>
								<u-icon label="口味收藏" size="15" name="/static/images/collect-off.png"></u-icon>
							</view>
						</view>
					</u-sticky>
					<u-read-more showHeight="70" color="#000" ref="uReadMore" closeText="展开更多">
						<view class="text-pd">
							<view class="bo-m">
								<text>【鲜活成分】</text>
								栀香绿茶x冷链鲜奶
							</view>
							<view class="bo-m">
								<text>【鲜活风味】</text>
								甄选栀香绿茶，花香清冽，搭配冷链鲜奶，顺滑鲜灵，满口栀子香，清爽更轻盈
							</view>
							<view class="bo-m">
								<text>【致敏原信息】</text>
								内含乳及乳制品，过敏者请谨慎选择
							</view>
							<view class="bo-m">
								<view>【杯型】</view>
								<text>【中杯500ml;大杯645ml】</text>
								*标注容量仅供参考，饮品量请以门店实际出品为准
							</view>
							<view class="bo-m">
								<text>【温馨小提示】</text>
								·纸杯款式请以门店实际出杯为准·为保证茶香纯粹、口感清爽，此饮品不可添加其他小料，敬请谅解·为品到更好茶香，请搭配使用轻乳茶专用三口管·由于配送时间较长，温/热饮到手温度将略有降低，敬请谅解·建议一小时内饮用风味更佳
							</view>
						</view>
						<u-parse @load="load"></u-parse>
					</u-read-more>
					<view class="bo-line"></view>
					<view class="goods-select">
						<!-- <u-sticky offset-top="126"> -->
						<view class="gs-title" v-if="isImageAbove===false">
							<view>口味选择</view>
							<u-icon label="口味收藏" size="15" name="/static/images/collect-off.png"></u-icon>
						</view>
						<!-- </u-sticky> -->
						<view class="gs-size">
							<view class="gs-stitle">规格(*必选)</view>
							<view class="gs-sbutton">
								<view class="gs-buttonbox">
									<u-button color="#99999a" type="primary" :plain="true" :hairline="true" text="中￥16"></u-button>
									<u-button type="primary" color="#bb78ba" :plain="true" :hairline="true" text="大￥18"></u-button>
								</view>
							</view>
						</view>
						<view class="gs-size">
							<view class="gs-stitle">加料(最多可选1份)</view>
							<view class="gs-sbutton">
								<view class="gs-buttonbox">
									<u-button color="#99999a" type="primary" :plain="true" :hairline="true" text="奶芙*有坚果碎¥5"></u-button>
									<u-button type="primary" color="#bb78ba" :plain="true" :hairline="true" text="奶芙*无坚果碎¥5"></u-button>
								</view>
							</view>
						</view>
						<view class="gs-size">
							<view class="gs-stitle">温度(*必选)</view>
							<view class="gs-sbutton">
								<view class="gs-buttonbox">
									<u-button color="#99999a" type="primary" :plain="true" :hairline="true" text="少冰"></u-button>
									<u-button type="primary" color="#bb78ba" :plain="true" :hairline="true" text="去冰"></u-button>
									<u-button color="#99999a" type="primary" :plain="true" :hairline="true" text="温"></u-button>
									<u-button color="#99999a" type="primary" :plain="true" :hairline="true" text="热"></u-button>
								</view>
							</view>
						</view>
						<view class="gs-size">
							<view class="gs-stitle">可选糖(*必选)</view>
							<view class="gs-sbutton">
								<view class="gs-buttonbox">
									<u-button color="#bb78ba" type="primary" :plain="true" :hairline="true" text="正常糖"></u-button>
									<u-button color="#99999a" type="primary" :plain="true" :hairline="true" text="七分糖"></u-button>
									<u-button color="#99999a" type="primary" :plain="true" :hairline="true" text="五分糖"></u-button>
								</view>
							</view>
						</view>
					</view>
					<!-- <view style="margin-top: 500rpx;"></view> -->
					<!-- 加入购物车 -->
					<view class="add-box">
						<view class="ab-top">
							<view><text style="font-size: 30rpx;">￥</text>16<text
									style="text-decoration: line-through;color: #a39d99; font-size: 30rpx; font-weight: 200;">￥18</text>
							</view>
							<u-number-box button-size="30" inputWidth="30" color="#ffffff" bgColor="#bb78ba"
								iconStyle="color: #fff"></u-number-box>
						</view>
						<view style="padding: 10rpx 20rpx 10rpx 20rpx;"><u-button type="primary" shape="circle" color="#bb78ba"
								size="large" text="加入购物车"></u-button></view>
					</view>
				</view>
			</scroll-view>
		</u-popup>

		<!-- 底部提示 -->
		<view class="bottom-tip">
			{{ bottomTipText }}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				containerHeight: 0, // 初始值为0，将在onLoad中获取
				scrollY: 0,
				scrollStep: 100, // 每次滑动的距离，单位rpx
				currentIndex: 0,
				isImageAbove: false,
				// 定义滚动超过这个高度，则出现那个吸顶的盒子
				imageTop: 270,
				// 这是一段很长的文字，也可能包含有HTML标签等内容
				show: false,
				currentTab: 'self', // self:自取  delivery:外卖
				bannerList: [],
				couponList: ['云岭茉莉白*中 9.9元喝', '云深幽兰11.9元喝'],
				categoryList: [],
				check: 0,
				goodsList: [],
				bottomTipText: '云深幽兰11.9元喝，今日可享受1单',
				pressTimer: null // 长按防抖定时器
			}
		},
		onLoad() {
			this.getBanner();
			this.getCategory();
			this.getGoodsList();
			// 获取容器高度
			this.getContainerHeight();
		},
		methods: {
			// getContainerHeight() {
			// 	// 创建选择器查询实例
			// 	const query = uni.createSelectorQuery().in(this);
			// },
			clickCategory(index) {
				this.currentIndex = index;
				// 点击后减少 translateY 的值，实现上滑效果
				this.scrollY -= this.scrollStep;

				// 可以添加点击效果的逻辑，比如记录点击的是哪个盒子
				// console.log(`点击了盒子: ${index + 1}`);

				// 这里写让盒子向上滑
				// // 通过ref获取导航容器元素​
				// const categoryNav = this.$refs.categoryNav;​
				// // 获取当前滚动距离​
				// const currentScrollTop = categoryNav.scrollTop;​
				// console.log(currentScrollTop);
				// // 向上滚动100px（可根据实际需求调整滚动距离）​
				// categoryNav.scrollTop = currentScrollTop - 100;
			},
			onScroll(e) {
				// 获取滚动距离
				const scrollTop = e.detail.scrollTop;
				// 判断图片是否在视口上方（考虑初始位置）
				this.isImageAbove = scrollTop > this.imageTop;
				// 调试输出
				// console.log(`滚动距离: ${scrollTop}px, 图片初始位置: ${this.imageTop}px, 是否在上方: ${this.isImageAbove}`);
			},
			load() {
				this.$refs.uReadMore.init();
			},
			handleAdd(goods) {
				console.log('添加商品到购物车', goods);
				// 按钮缩放反馈
				this.$refs.addIcon.scale = 0.9;
				setTimeout(() => {
					this.$refs.addIcon.scale = 1;
				}, 200);
			},
			handlePress(goods, e) {
				// 长按逻辑（可选：如显示商品详情）
				this.pressTimer = setTimeout(() => {
					console.log('长按商品', goods);
					// 可触发弹窗或路由跳转
				}, 500);
			},
			handleRelease() {
				clearTimeout(this.pressTimer);
			},
			goSearch() {
				uni.navigateTo({
					url: '/pages/edit-pen/search'
				});
			},
			async getBanner() {
				// let\const
				// 发起请求，用result接后端的数据
				let result = await this.$u.api.user.banner({});
				// 后端返回successs这code为1
				if (result.code == 1) {
					// 这里接到后端传回的数据
					let bannerData = result.data.banner
					// console.log(bannerData)
					// forEach用法，跟之前差不多。bannerData是数组
					bannerData.forEach(item => {
						// console.log(item.url_text);
						// 这里是上面data()里定义的swiperList,然后追加进去
						this.bannerList.push(item.url_text)
					});
				}

			},
			// 跳转地址
			async getAddress() {
				await uni.navigateTo({
					url: '/pages/edit-pen/shops'
				})
			},
			// 获取商品分类
			async getCategory() {
				// console.log(111);
				let result = await this.$u.api.goods.categories({});
				if (result.code) {
					// console.log(result.data);
					this.categoryList = result.data
				}
			},
			// 获取商品列表
			async getGoodsList() {
				let result = await this.$u.api.goods.getProudcts({});
				if (result.code) {
					console.log(result.data)
					this.goodsList = result.data
					this.goodsList.forEach(item => {
						// 判断是否有标签
						if (item.good_tab.desc !== null) {
							// 以逗号作为分割
							item.tag = item.good_tab.desc.split(",");
						}
					})
				}
			},
			open() {
				// console.log('open');
			},
			close() {
				this.show = false
				// console.log('close');
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 定义主题颜色变量
	$primary-color: #b92b27; // 主色调-红色
	$secondary-color: #edcc7b; // 辅助色-金色
	$text-color: #333; // 文本主色
	$text-light: #666; // 文本浅色
	$bg-light: #f8f8f8; // 浅色背景

	.home-page {
		background-color: #fff;
		height: 100%;
		background: linear-gradient(to bottom, $primary-color 0%, #fff 200rpx);

		.top-nav {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			position: absolute;
			top: 75rpx;
			left: 30rpx;
			z-index: 100;

			.tab-top {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 160rpx;
				border-radius: 66rpx;
				margin-right: 10rpx;
				height: 50rpx;
				padding: 5rpx;
				background-color: rgba(255, 255, 255, 0.5);

				.nav-tab1 {
					text-align: center;
					line-height: 50rpx;
					height: 50rpx;
					width: 80rpx;
					font-size: 0.6rem;
					border-radius: 50rpx;
					transition: all 0.5s ease;

					&.active {
						background-color: #000;
						color: #fff;
					}
				}
			}

			.search-icon {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 60rpx;
				height: 60rpx;
				border-radius: 50%;
				background-color: rgba(255, 255, 255, 0.5);
				margin-left: auto;
				margin-right: 20rpx;
			}
		}

		.banner-swiper {
			height: 380rpx;

			.banner-img {
				width: 100%;
				height: 100%;
			}
		}

		.store-info {
			position: relative;
			margin: -30rpx 0 0;
			padding: 20rpx;
			background-color: #fff;
			border-radius: 20rpx 20rpx 0 0;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

			.store-header {
				display: flex;
				align-items: center;
				font-size: 32rpx;
				font-weight: bold;
				color: $primary-color;

				.star-icon {
					color: $secondary-color;
					margin-right: 8rpx;
					font-size: 28rpx;
				}

				.arrow-icon {
					margin-left: auto;
					color: #999;
					transition: all 0.3s;

					&:hover {
						transform: translateX(5rpx);
					}
				}
			}

			.store-meta {
				display: flex;
				align-items: center;
				margin-top: 8rpx;
				font-size: 26rpx;
				color: $text-light;

				.make-tag {
					background-color: #d5e878;
					border-radius: 5rpx;
					padding: 5rpx;
					font-size: 1rem;
				}

				.distance-tag {
					color: #999b9c;
				}

				.divider {
					margin: 0 6rpx;
					color: #ccc;
				}
			}

			.coupon-list {
				display: flex;
				flex-wrap: wrap;
				margin-top: 16rpx;

				.coupon-item {
					background-color: #fff0f0;
					color: $primary-color;
					padding: 8rpx 18rpx;
					border-radius: 30rpx;
					font-size: 24rpx;
					margin-right: 12rpx;
					margin-top: 10rpx;
					box-shadow: 0 2rpx 6rpx rgba(185, 43, 39, 0.1);
					transition: all 0.3s;

					&:hover {
						transform: scale(1.05);
					}
				}
			}
		}

		.activity-banner {
			width: 100%;
			height: auto;
			margin: 20rpx;
			border-radius: 12rpx;
		}

		.parent {
			display: flex;
			justify-content: space-around;
			align-items: flex-start;
			height: 100%;
			width: 100%;

			.category-nav {
				background-color: #fff;
				height: 100%;
				width: 100%;
				min-height: 500rpx;
				transition: all 0.3s linear;

				.category-item {
					// display: flex;
					width: 100%;
					text-align: center;
					flex-direction: column;
					background-color: #ffffff;
					align-items: center;
					padding: 10rpx 0 30rpx 0;
					transition: all 0.3s linear;

					&.active {
						background-color: #fef2f2;
						transform: translateY(10%);
					}

					.category-icon {
						width: 70rpx;
						height: 70rpx;
						margin-bottom: 8rpx;
					}

					.category-text {
						font-size: 24rpx;
						color: $text-color;
					}
				}
			}


			.goods-list {
				// padding: 20rpx;

				.goods-item {
					display: flex;
					width: 100%;
					justify-content: space-between;
					// margin: 30rpx;
					padding: 10rpx;
					align-items: center;
					border-bottom: 1px solid #f5f5f5;
					transition: all 0.3s;

					&:active {
						transform: translateY(-2rpx);
						box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
					}

					.goods-img {
						width: 100%;
						// width: 180rpx;
						// height: 180rpx;
						margin-right: 20rpx;
						border-radius: 12rpx;
					}

					.goods-info {
						display: flex;
						height: 100%;
						flex-direction: column;
						justify-content: space-between;
						// width: 100%;
						padding: 10rpx;

						.goods-name {
							font-size: 25rpx;
							font-weight: bold;
							color: $primary-color;
						}

						.tags {
							display: block;
							/* 块级容器 */
							white-space: nowrap;
							/* 禁止换行 */
							overflow: hidden;
							width: 95%;

							.tag {
								display: inline-block;
								// float: left;
								background-color: #fef2f2;
								color: $primary-color;
								padding: 4rpx 12rpx;
								border-radius: 12rpx;
								font-size: 16rpx;
								margin-right: 10rpx;
								margin-bottom: 5rpx;
								margin-top: 5rpx;
							}
						}

						.desc {
							font-size: 20rpx;
							color: $text-light;
						}

						.price {
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-bottom: 10rpx;

							.current-price {
								font-size: 20rpx;
								color: #e60012;
								font-weight: bold;
								margin-right: 10rpx;
							}

							.original-price {
								font-size: 26rpx;
								color: #999;
								text-decoration: line-through;
							}
						}

						.addcart {
							float: right;
							margin-right: 10rpx;
							font-weight: bolder;
							position: absolute;
							right: 0;
							bottom: 5rpx;
							color: $secondary-color;
						}
					}
				}
			}
		}

		.bo-m {
			margin-top: 20rpx;
			// padding: 0 40rpx;
		}

		.text-pd {
			padding: 0 40rpx;
			text-indent: 0em;
		}

		.bo-line {
			border-bottom: #f7f7f7 solid 1rpx;
			margin-top: 50rpx;
			margin-bottom: 20rpx;
		}

		.goods-select {
			padding: 0 30rpx 0 40rpx;
			margin-bottom: 100rpx;

			.gs-title {
				padding: 0 10rpx 20rpx 0;
				background-color: #fff;
				display: flex;
				justify-content: space-between;
				flex-direction: row;
				align-items: center;
				font-size: 32rpx;
			}

			.mg-bottom {
				margin-bottom: 600rpx;
			}

			.gs-size {
				margin-top: 30rpx;

				.gs-stitle {
					font-size: 28rpx;
					color: #99999a;
				}

				.gs-sbutton {
					display: flex;
					flex-direction: row;

					.gs-buttonbox {
						display: flex;
						gap: 20rpx;
						// display: grid;
						// grid-template-columns: repeat(4, 1fr);
						// gap: 20rpx;
						/* 列间距 */
						// grid-column-gap: 20rpx;
						// grid-row: 10;
						// display: flex;
						// flex-direction: row;
						margin: 20rpx 0 40rpx 0rpx;

						// .btn-mr {
						// 	margin-right: 20rpx;
						// }
					}
				}
			}
		}

		.sm-goods {
			display: flex;
			align-items: left;
			padding: 20rpx;
			background-color: #fff;
			border-bottom: #ebebec solid 1rpx;

			.sm-gtitle {
				display: flex;
				flex-direction: column;
				justify-content: space-evenly;
				align-items: left;
				padding: 20rpx;

				.tit {
					font-size: 40rpx;
					font-weight: bolder;
				}

				.txt {
					color: #999a9a;
					font-size: 28rpx;
				}
			}
		}

		.gs-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 10rpx 40rpx 20rpx 10rpx;
			background-color: #fff;
			border-bottom: #ebebec solid 1rpx;
		}

		.goName {
			font-size: 40rpx;
			margin-top: 40rpx;
			padding: 0 50rpx;
		}

		// 底部加入购物车
		.add-box {
			width: 100%;
			position: fixed;
			bottom: 0;
			z-index: 2000;
			border-top: #f7f7f7 solid 1rpx;
			background-color: #ffffff;

			.ab-top {
				display: flex;
				justify-content: space-between;
				font-size: 40rpx;
				font-weight: bold;
				color: #e25e5c;
				padding: 10rpx 20rpx 10rpx 20rpx;
			}
		}

		.bottom-tip {
			position: fixed;
			width: 100%;
			bottom: 0;
			z-index: 100;
			text-align: center;
			padding: 20rpx;
			font-size: 28rpx;
			color: $text-light;
			background-color: $bg-light;
		}
	}
</style>